package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Slf4j
@Component
/**
 * 创建订单操作的认证
 */
public class ValidateHandler extends AbstractTransHandler {


    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        Long userId = createOrderContext.getUserId();
        Long addressId = createOrderContext.getAddressId();
        String tel = createOrderContext.getTel();
        String userName = createOrderContext.getUserName();
        String streetName = createOrderContext.getStreetName();
        BigDecimal orderTotal = createOrderContext.getOrderTotal();
        if(userId==null||addressId==null||
                StringUtils.isBlank(tel)|| StringUtils.isBlank(userName)|| StringUtils.isBlank(streetName)||orderTotal==null){
            throw new ValidateException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        return true;
    }
}
